# -*- coding:utf-8 -*-
"""
Created on 10 jun 2012

@author: Olof Manbo
"""

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

import logging
import models


class MainPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            if users.is_current_user_admin():
                global_data_key = db.Key.from_path('GlobalData', "Global")
                global_data = db.get(global_data_key)
                if global_data is None:
                    global_data = models.GlobalData(key_name = "Global")
                    global_data.nextPlayerId = 1
                    global_data.put()
                html = open("html/index.html", 'r').read()
                logging.info("user id:%s"%user.user_id())
                admin_key = db.Key.from_path('Administrator', user.user_id())
                admin = db.get(admin_key)
                logging.info(admin)
                if admin is None:
                    admin = models.Administrator(key_name = user.user_id())
                    admin.loginName = user.nickname()
                    admin.put()
                self.response.out.write(html)
            else:
                self.response.out.write("You are not allowed to play this game.")
        else:
            self.redirect(users.create_login_url(self.request.uri))

class PlayerForm(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        logging.info(user.nickname())
        if user:
            if users.is_current_user_admin():
                logging.info(user.nickname())
                admin_key = db.Key.from_path('Administrator', user.user_id())
                admin = db.get(admin_key)
                logging.info(admin)

                if admin is not None:
                    logging.info(self.request.get('name'))

                    
                    global_data_key = db.Key.from_path('GlobalData', "Global")
                    global_data = db.get(global_data_key)
                    

                    
                    logging.info(admin.players)
                    logging.info(global_data.nextPlayerId)
                    newPlayer = models.Player(key_name = str(global_data.nextPlayerId))
                    admin.players.append(self.request.get('name'))
                    admin.playerIds.append(str(global_data.nextPlayerId))
                    admin.put()
                    global_data.nextPlayerId += 1
                    global_data.put()
                    newPlayer.loginName = user.nickname()
                    newPlayer.userName = self.request.get('name')
                    logging.info(self.request.get('name'))
                    newPlayer.put()
                    self.response.out.write(self.request.get('name'))
        else:
            self.redirect(users.create_login_url(self.request.uri))